<?php


/*
 *
 * Moze dodawac/edytowac/usuwac tylko wykladowcow!
 *
 */
class Technical_AccountController extends Zend_Controller_Action
{

	public function init()
	{
		/* Initialize action controller here */
	}

	public function indexAction()
	{
		$session = new Zend_Session_Namespace('identity');
		$users = new Application_Model_Dao_User();
		if ($this->_getParam('lecturer') == 'true') {
			$result = $users->getAllLecturer();
			$this->view->head = "Wykładowcy";
			$this->view->users = $result;
		} else {
			$result = $users->getAllUser();
			$this->view->head = "Użytkownicy";
			$this->view->users = $result;
		}
		if($result == null){
			$this->view->empty = "Brak użytkowników w bazie.";
		}else{
			$this->view->empty = "";
		}
		$designation = $this->_getParam('designation');
		if(isset($session->statusdel) && $this->_getParam('statusdelete') == 'true') {
			$this->view->delete = "Rekord \"".$designation."\" został poprawnie usunięty.";
			unset($session->statusdel);
		}else{
			$this->view->delete = "";
		}

		if( isset( $session->attention ) ) {
			$this->view->attention = $session->attention ;
			unset( $session->attention );
		} else {
			$this->view->attention = "";
		}

		if( isset( $session->operationsuccess ) ) {
			$this->view->operationStatus = $session->operationsuccess ;
			unset( $session->operationsuccess );
			if(isset( $session->operationdesignation ) ) {
				$this->view->operationDesignation = $session->operationdesignation;
				unset( $session->operationdesignation );
			} else {
				$this->view->operationDesignation = "";
			}
		} else {
			$this->view->deleteRecord = "";
		}
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_Account();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		// edycja oraz dodawanie w jquery, kod obsługujący requesta z danymi z formularza, zapis do bazy
		if( $this->getRequest()->isPost() ) {
			$formData = $this->getRequest()->getPost();
			if( $form->isValid($formData) ) {
				$user = new Application_Model_Entities_User();
				$user->setId($form->getValue('id'));
				$user->setAcademicDegree( $form->getValue('academicDegree_id') );
				$user->setName( $form->getValue('name') );
				$user->setSurname( $form->getValue('surname') );
				$user->setEmail( $form->getValue('email') );
				$user->setPassword( $form->getValue('password') );
				$user->setCathedral( $form->getValue('cathedral_id') );
				$user->setRole( $form->getValue('role_id') );
				$user->setLecturer( $form->getValue('lecturer') );
				$userDao = new Application_Model_Dao_User();
				if($form->getValue('typeForm') == 'editform' ) {
					$result = $userDao->updateUser($user);
					if($result > 0 ) {
						$session->operationsuccess = 'updatesuccess';
					} else {
						$session->operationsuccess = 'updatefail';
					}
				} else {
					$session->attention = 'Błędne dane dotyczące typu operacji';
				}
				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		} else {
			$session->attention = '';
		}

	}

	public function addAction()
	{
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_Account();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		$form->getElement('typeForm')->setValue('addform');
		$form->submit->setLabel('Dodaj nowego użytkownika');
		$request = $this->getRequest();
		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$user = new Application_Model_Entities_User();
				$user->setAcademicDegree( $form->getValue('academicDegree_id') );
				$user->setName( $form->getValue('name') );
				$user->setSurname( $form->getValue('surname') );
				$user->setEmail( $form->getValue('email') );
				$user->setPassword( $form->getValue('password') );
				$user->setCathedral( $form->getValue('cathedral_id') );
				$user->setRole( $form->getValue('role_id') );
				$user->setLecturer( $form->getValue('lecturer') );
				$userDao = new Application_Model_Dao_User();
				$result = $userDao->saveUser($user);
				if($result > 0 ) {
					$session->operationsuccess = 'addsuccess';
					$session->operationdesignation = $form->getValue('name')." ".$form->getValue('surname');
				} else {
					$session->operationsuccess = 'addfail';
				}
				//	$result = Zend_Auth::getInstance()->authenticate($user);
				//if ($result->isValid()) {
				$this->_helper->redirector('index');
				//} else {
				//$form->login->addError('Należy wypełnić wszystkie pola.');
				//}
			} else {
				$session->attention = 'Przesłane dane w formularzu nie są poprawne.';
				$this->_helper->redirector( 'index');
			}
		} else {
			$session->attention = '';
			//$this->_helper->redirector( 'index');
		}
		$this->view->AccountForm = $form;
	}

	public function editAction()
	{
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_Account();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		$form->submit->setLabel('Zatwierdź zmiany');
		$this->view->AccountEditForm = $form;
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$user = new Application_Model_Entities_User();
				$user->setId($form->getValue('id'));
				$user->setAcademicDegree( $form->getValue('academicDegree_id') );
				$user->setName( $form->getValue('name') );
				$user->setSurname( $form->getValue('surname') );
				$user->setEmail( $form->getValue('email') );
				$user->setPassword( $form->getValue('password') );
				$user->setCathedral( $form->getValue('cathedral_id') );
				$user->setRole( $form->getValue('role_id') );
				$user->setLecturer( $form->getValue('lecturer') );
				$userDao = new Application_Model_Dao_User();
				$userDao->updateUser($user);
				$this->_helper->redirector('index');
			} else
			$form->populate($formData);
		} else {
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$userDao = new Application_Model_Dao_User();
				$form->populate($userDao->getUserByID($id));
				$session->operationdesignation = $form->getValue('name')." ".$form->getValue('name');
				$form->getElement('typeForm')->setValue('editform');
			} else {
				$session->attention = 'Błąd podczas pobierania danych lub podczas przypisywania ich do pól formularza.';
				$this->_helper->redirector( 'index');
			}
		}
	}

	public function deleteAction(){
		$session = new Zend_Session_Namespace('identity');
		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del');
			if ($del == 'Tak, usuń rekord') {
				$id = $request->getPost('id');
				$designation = $request->getPost('designation');
				$user = new Application_Model_Entities_User();
				$user->setId($id);
				$userDao = new Application_Model_Dao_User();
				$userDao->deleteUser($user);
				$session = new Zend_Session_Namespace('identity');
				$session->statusdel = 'true';
				$this->_helper->redirector( 'index', 'account', 'technical', array('statusdelete' => 'true', 'designation' => $designation));
			}else
			$this->_helper->redirector( 'index');
				
		} else if($this->_getParam('jsflag') == 'false' ){
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$userDao = new Application_Model_Dao_User();
				$result = $userDao->getRowUser($id);
				$this->view->users = $result;
			}
		} else if($this->_getParam('jsflag') == 'true' ){
			$id = $this->_getParam('id', 0);
			$user = new Application_Model_Entities_User();
			$user->setId($id);
			$userDao = new Application_Model_Dao_User();
			$result = $userDao->deleteUser($user);
			if($result > 0) {
				$session->operationsuccess = "deletesuccess" ;
				$session->operationdesignation = $this->_getParam('name', 0);
			} else {
				$session->operationdesignation = "";
				$session->operationsuccess = "deletefail" ;
			}
		} else {
			$this->_helper->redirector('index');
		}
	}

}



